﻿using System;
using LMD.Lib.DataClass;
using System.Web.UI.WebControls;
using System.Collections.Generic;
using System.Data;

public partial class NPS_app_Survey_InputHasil : PageBase
{
    private void Clear()
    {
        lblInfo.Text = string.Empty;
        txtName.Text = string.Empty;
        txtKelas.Text = string.Empty;
        txtPerusahaan.Text = string.Empty;
        txtPosition.Text = string.Empty;
        txtSearchCompany.Text = string.Empty;
        txtSearchName.Text = string.Empty;
        txtSegment.Text = string.Empty;
        ddlKategori.SelectedIndex = 0;
        ltScore.Text ="0";
        hfScore.Value = "0";
        ddlReason.SelectedIndex = 0;
        txtTgl.Clear();
        txtType.Text = string.Empty;
        showForm(false);
    }

    private void loadSearchForm()
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        gvSearch.DataSource = ncc.NPSCustomerContactList;
        gvSearch.DataBind();
    }

    private void loadForm()
    {
        getKategori();
        getReason();
    }
    private void getReason()
    {
        Controller.NPSReasonController ctrNPS = new Controller.NPSReasonController();
        List<Entities.NPSReason> listNPS = new List<Entities.NPSReason>();
        listNPS = ctrNPS.NPSReasonList;
        ddlReason.Items.Clear();
        ddlReason.DataSource = listNPS;
        ddlReason.DataBind();
    }

    private void getKategori()
    {
        Controller.NPSCPCategoryController ctrNPS = new Controller.NPSCPCategoryController();
        List<Entities.NPSCPCategory> listNPS = new List<Entities.NPSCPCategory>();
        listNPS = ctrNPS.NPSCPCategoryList;
        ddlKategori.Items.Clear();
        ddlKategori.DataSource = listNPS;
        ddlKategori.DataBind();

    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loadSearchForm();
            loadForm();
            Clear();
        }

        // srcContact.Condition = string.Empty;
    }



    protected void btnSave_Click(object sender, EventArgs e)
    {
        Controller.NPSTypeController tc = new Controller.NPSTypeController();
        int type = tc.getNPSType(hfType.Value).iID;
        Entities.NPSSurvey entNPS = new Entities.NPSSurvey
        {
            iScore = Convert.ToInt32(hfScore.Value),
            iNPSReasonID = Convert.ToInt32(ddlReason.SelectedValue),
            iNPSCPCategoryID = Convert.ToInt32(ddlKategori.SelectedValue),
            iNPSTypeID = type,
            dtmSurveyDate = txtTgl.SelectedDate,
            iContactPersonID = Convert.ToInt32(hfID.Value),
            sxCreator = ICPUser.ID
        };
        Controller.NPSSurveyController ctrNPS = new Controller.NPSSurveyController();
        ctrNPS.InsertNPSSurveyData(entNPS);
        Clear();
        AlertNotification("Survey baru sudah dimasukkan");
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        search();
    }

    private void search()
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        gvSearch.DataSource = ncc.getNPSCustomerContactList(txtSearchName.Text, txtSearchCompany.Text);
        gvSearch.DataBind();
    }
    protected void gvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "choose")
        {
            bindData(e.CommandArgument.ToString());
        }
    }

    protected void bindData(string id)
    {
        Controller.NPSCustomerContactController ncc = new Controller.NPSCustomerContactController();
        Entities.NPSCustomerContact contact = ncc.getNPSCustomerContact(Convert.ToInt32(id), false);
        txtName.Text = contact.sxPICName;
        txtKelas.Text = contact.sxCompanyClass;
        txtPerusahaan.Text = contact.sxCompanyName;
        txtPosition.Text = contact.sxPosition;
        txtSegment.Text = contact.sxCompanySegment;
        hfID.Value = id;
        showForm(true);
    }

    protected void showForm(bool isShow)
    {
        pnlInfo.Visible = isShow;
        pnlInfo2.Visible = isShow;
    }
    protected void gvSearch_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvSearch.PageIndex = e.NewPageIndex;
        gvSearch.DataBind();
    }
}
